package com.fulihui.wgj.biz.common.util;

import com.fulihui.weixinsdk.model.TemplateItem;
import com.fulihui.wgj.dal.mybatis.domain.WxTemplate;

import java.util.Map;
import java.util.Objects;

/**
 * 模板内容渲染工具类
 *
 * @author wang_wx
 * @date 2018-08-06
 */
public class TemplateRenderUtil {

    /**
     * 渲染模板内容
     *
     * @param data       模板数据
     * @param wxTemplate 模板对象
     * @return
     */
    public static String render(Map<String, TemplateItem> data, WxTemplate wxTemplate) {
        if (Objects.nonNull(wxTemplate) && data.size() > 0) {
            String title = wxTemplate.getTitle();
            String content = wxTemplate.getContent();
            for (String key : data.keySet()) {
                content = content.replace("{{" + key + "." + "DATA}}", "<span style='color:" + data.get(key).getColor() + "'>" + data.get(key).getValue() + "</span>");
            }
            content = content.replaceAll("\n", "<br/>");
            content = title + "<br/>" + content;
            return content;
        }
        return null;
    }
}
